package com.besmart.workflow.core.service.factory.extractor;

import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.besmart.workflow.core.exception.ConditionExtractException;
import com.besmart.workflow.core.model.ConditionNode;
import com.besmart.workflow.core.model.Constants;
@Service
public class ConditionExtractor {

	private static final String INVALID_CONDITION_NAME_MESSAGE = "Invalid condition node name, please check the input node name is: condition";

	public ConditionNode extract(Element conditionNode) throws ConditionExtractException {
		if(conditionNode.getName().equals(Constants.CONDITION_NAME))
		{
			String trigger = conditionNode.attributeValue(Constants.CONDITION_TRIGGER);
			String output = conditionNode.attributeValue(Constants.CONDITION_OUTPUT);
			String nextOperation = conditionNode.attributeValue(Constants.CONDITION_NEXT_OPERATION);
			ConditionNode condition = new ConditionNode(trigger, output, nextOperation);
			return condition ;
		}
		else
		{
			throw new ConditionExtractException(INVALID_CONDITION_NAME_MESSAGE);
		}
	}

}
